package cn.itcast.service;

import cn.itcast.interfaces.CheckItemService;
import cn.itcast.interfaces.PermissionService;
import cn.itcast.mapper.CheckItemMapper;
import cn.itcast.mapper.PermissionMapper;
import cn.itcast.pojo.CheckItem;
import cn.itcast.pojo.PageResult;
import cn.itcast.pojo.Permission;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author: peterChen
 * @date: 2019/8/13 12:11
 * @description: TODO
 * @version: 1.0
 */
@Service(interfaceClass = PermissionService.class)
@Transactional
public class PermissionServiceImpl implements PermissionService {
    @Autowired
    private PermissionMapper permissionMapper;

    /**
     * 分页查询权限数据
     * @param currentPage
     * @param pageSize
     * @param queryString
     * @return
     */
    @Override
    public PageResult pageQuery(Integer currentPage, Integer pageSize, String queryString) {
        PageHelper.startPage(currentPage,pageSize);
        Page page = permissionMapper.selectByContition(queryString);
        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 新增权限
     * @param permission
     */
    @Override
    public void add(Permission permission) {
        permissionMapper.add(permission);
    }

    /**
     * 根据id获取权限数据并回显
     * @param id
     * @return
     */
    @Override
    public Permission findPermissionById(Integer id) {
        return permissionMapper.findPermissionById(id);
    }

    /**
     * 编辑权限
     * @param permission
     */
    @Override
    public void edit(Permission permission) {
        permissionMapper.edit(permission);
    }

    /**
     * 根据id删除权限，首先确定是否为被关联的权限
     * @param id
     */
    @Override
    public void delete(Integer id) {
        long count = permissionMapper.findPermissionByPermissionId(id);
        if(count>0){
            //表示该检查项被关联，无法删除
            throw new RuntimeException("该权限被引用，无法删除！");
        }
        //无关联。直接删除
        permissionMapper.delete(id);
    }

    /**
     * 查询所有权限
     * @return
     */
    @Override
    public List<Permission> findAll() {
        return permissionMapper.findAll();
    }
}
